package com.briup.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.briup.comon.BaseContext;
import com.briup.comon.R;
import com.briup.entity.ShoppingCart;
import com.briup.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Auther: liao
 * @Date: 2022/9/15-09-15-9:41
 * @Description：com.briup.controller
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 加入购物车，如果购物车里面已经有了，就添加数量
     * @param shoppingCart
     * @param session
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> save(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        
        //通过session获取当前用户加入购物车的id
        Long userId = (Long) session.getAttribute("user");
        
        shoppingCart.setUserId(userId);
        //判断当前加入购物车的菜品或套餐在购物车是否存在，存在的话就添加数量

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper();
        Long dishId = shoppingCart.getDishId();
        if (dishId!=null){
            //判断当前加入的是菜品还是套餐,不等于空时是菜品
            //听过菜品id查询购物车中有没有这个菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
            
        }else {
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            
        }
        //通过菜品id或者套餐id得到一个对象
          ShoppingCart shoppingCartObj = shoppingCartService.getOne(queryWrapper);
        
            if (shoppingCartObj==null){
                //该菜品或者套餐不存在
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartService.save(shoppingCart);
               
            }else {
                //该菜品或者套餐已经存在
              shoppingCartObj.setNumber(shoppingCartObj.getNumber()+1);
                shoppingCartService.updateById(shoppingCartObj);
                shoppingCartObj=shoppingCart;
               
            }

            return R.success(shoppingCartObj);
       
    }

    /**
     * 根据用户id查询当前购物车的所有东西
     * @param session
     * @return
     */
    
    @GetMapping("/list")
    public R<List<ShoppingCart>> getAll(HttpSession session){
        
        //获取用户id
        Long userId = (Long) session.getAttribute("user");
        //根据用户id查询当前购物车的所有东西
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 通过用户id删除购物车所有东西
     * @return
     */
    
    @DeleteMapping("/clean")
    public R<String> deleteAll(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
      return   R.success("清空购物车成功！");
        
    }

    /**
     * 减少购物车的数量,数量等于0时直接删除
     * @param shoppingCart
     * @return
     */
    
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        
        
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper();

        Long dishId = shoppingCart.getDishId();
        if (dishId!=null){
            //当前减的数量是菜品的数量
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //当前减的数量是套餐的数量
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        
        cart.setNumber(cart.getNumber()-1);
        
        shoppingCartService.updateById(cart);
        shoppingCart=cart;
        
        //如果菜品或套餐的数量等于0，就通过菜品id或者套餐id删除
        if (shoppingCart.getNumber()==0){
            shoppingCartService.remove(queryWrapper);
        }
        
        return R.success(shoppingCart);
    }
    
    
    
    
}
